#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <iconv.h>
bool unicode_to_utf8 (char *inbuf, size_t *inlen, char *outbuf, size_t *outlen)
{
  /* 目的编码, TRANSLIT：遇到无法转换的字符就找相近字符替换
   *           IGNORE ：遇到无法转换字符跳过*/
  char *encTo = "UTF-8//IGNORE";
/* 源编码 */
  char *encFrom = "UNICODE";

  /* 获得转换句柄
   *@param encTo 目标编码方式
   *@param encFrom 源编码方式
   *
   * */
  iconv_t cd = iconv_open (encTo, encFrom);
  if (cd == (iconv_t)-1)
  {
     perror ("iconv_open");
  }

  /* 需要转换的字符串 */
  printf("inbuf=%s\n", inbuf);

  /* 打印需要转换的字符串的长度 */
  printf("inlen=%d\n", *inlen);


  /* 由于iconv()函数会修改指针，所以要保存源指针 */
  char *tmpin = inbuf;
  char *tmpout = outbuf;
  size_t insize = *inlen;
  size_t outsize = *outlen;

  /* 进行转换
   *@param cd iconv_open()产生的句柄
   *@param srcstart 需要转换的字符串
   *@param inlen 存放还有多少字符没有转换
   *@param tempoutbuf 存放转换后的字符串
   *@param outlen 存放转换后,tempoutbuf剩余的空间
   *
   * */
  size_t ret = iconv (cd, &tmpin, inlen, &tmpout, outlen);
  if (ret == -1)
  {
     perror ("iconv");
  }

  /* 存放转换后的字符串 */
  printf("outbuf=%s\n", outbuf);

  //存放转换后outbuf剩余的空间
  printf("outlen=%d\n", *outlen);

  int i = 0;

  for (i=0; i<(outsize- (*outlen)); i++)
  {
     //printf("%2c", outbuf[i]);
     printf("%x\n", outbuf[i]);
  }

  /* 关闭句柄 */
  iconv_close (cd);

  return 0;
}

bool utf8_to_unicode (char *inbuf, size_t *inlen, char *outbuf, size_t *outlen)
{

  /* 目的编码, TRANSLIT：遇到无法转换的字符就找相近字符替换
   *           IGNORE ：遇到无法转换字符跳过*/
  char *encTo = "UNICODE//IGNORE";
  /* 源编码 */
  char *encFrom = "UTF-8";

  /* 获得转换句柄
   *@param encTo 目标编码方式
   *@param encFrom 源编码方式
   *
   * */
  iconv_t cd = iconv_open (encTo, encFrom);
  if (cd == (iconv_t)-1)
  {
      perror ("iconv_open");
  }

  /* 需要转换的字符串 */
  printf("inbuf=%s\n", inbuf);

  /* 打印需要转换的字符串的长度 */
  printf("inlen=%d\n", *inlen);

  /* 由于iconv()函数会修改指针，所以要保存源指针 */
  char *tmpin = inbuf;
  char *tmpout = outbuf;
  size_t insize = *inlen;
  size_t outsize = *outlen;

  /* 进行转换
   *@param cd iconv_open()产生的句柄
   *@param srcstart 需要转换的字符串
   *@param inlen 存放还有多少字符没有转换
   *@param tempoutbuf 存放转换后的字符串
   *@param outlen 存放转换后,tempoutbuf剩余的空间
   *
   * */
  size_t ret = iconv (cd, &tmpin, inlen, &tmpout, outlen);
  if (ret == -1)
  {
     perror ("iconv");
  }

  /* 存放转换后的字符串 */
  printf("outbuf=%s\n", outbuf);

  //存放转换后outbuf剩余的空间
  printf("outlen=%d\n", *outlen);

  int i = 0;

  for (i=0; i<(outsize- (*outlen)); i++)
  {
     //printf("%2c", outbuf[i]);
     printf("%x\n", outbuf[i]);
  }

  /* 关闭句柄 */
  iconv_close (cd);

  return 0;
}

int code_convert(const char *from_charset, const char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
	iconv_t cd;
	char **pin = &inbuf;
	char **pout = &outbuf;

	cd = iconv_open(to_charset, from_charset);
	if (cd == (iconv_t)-1)
		return 0;
	
	memset(outbuf, 0, outlen);
	if (iconv(cd, pin, &inlen, pout, &outlen) != 0)
		return 0;
	
	iconv_close(cd);

	return 1;
}

int coding_convert(const char *from_charset, const char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
	iconv_t cd;
	char **pin = &inbuf;
	char **pout = &outbuf;
	int ret = 0;

	printf("bobdbg---> %s %u\n", __FUNCTION__, __LINE__);
	cd = iconv_open(to_charset, from_charset);
	if (cd == NULL)
		return 0;

	printf("bobdbg---> %s %u\n", __FUNCTION__, __LINE__);
	memset(outbuf, 0, outlen);
	ret = iconv(cd, pin, &inlen, pout, &outlen);
	printf("bobdbg---> ret = [%d]\n", ret);
	if (ret < 0)
		return 0;

	printf("bobdbg---> %s %u\n", __FUNCTION__, __LINE__);
	iconv_close(cd);
	return 1;
}

int main ()
{
  /* 需要转换的字符串 */
  //char inbuf[1024] = "abcdef哈哈哈哈行"; 
  char *text = "汉字转码测试";
    
  char inbuf[1024] = {};
  strcpy (inbuf, text);
  size_t inlen = strlen (inbuf);

  /* 存放转换后的字符串 */
  char outbuf[1024] = {0};
  size_t outlen = 1024;

  char outbuf1[1024] = {0};
  size_t outlen1 = 1024;

  int ret = 0;

  //utf8_to_unicode (inbuf, &inlen, outbuf, &outlen);
  //printf ("print outbuf: %s\n", outbuf);

  //code_convert("UTF-8","UNICODE//IGNORE",inbuf,&inlen,outbuf,&outlen);
  //ret = code_convert("UTF-8","UNICODE//IGNORE",inbuf,strlen(inbuf),outbuf,sizeof(outbuf));
  ret = coding_convert("UTF-8","UNICODE",inbuf,strlen(inbuf),outbuf,sizeof(outbuf));
  printf ("--- code convert finished\n");
  printf ("print inbuf: %s\n", inbuf);
  printf ("print outbuf: %s\n", outbuf);

  //ret = code_convert("UTF-8", "UNICODE//IGNORE", outbuf, sizeof(outbuf), outbuf1, sizeof(outbuf1));
  ret = coding_convert("UNICODE", "UTF-8", outbuf, sizeof(outbuf), outbuf1, sizeof(outbuf1));
  printf ("ret = %d\n", ret);
  printf ("print inbuf: %s\n", outbuf);
  printf ("print outbuf: %s\n", outbuf1);

#if 0
  size_t outsize = strlen(outbuf);
  size_t insize = 1024;
  char instr[1024] = {};
  unicode_to_utf8 (outbuf, &outsize, instr, &insize);
  printf ("print buf: %s\n", instr);
#endif
  return 0;
}
